package com.eqdd.yiqidian.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;

import com.eqdd.library.percentUtil.WindowUtil;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.FloatingItemDecoration;
import com.gamerole.itemdecoration.lib.DecorationDrawer;
import com.gamerole.itemdecoration.lib.PinHeadItemDecoration;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Flowable;

/**
 * Created by lv on 17-8-23.
 */

public class ItemDecorationUtil {
    public static FloatingItemDecoration openFloatDeco(Context context, RecyclerView recyclerView) {
        FloatingItemDecoration floatingItemDecoration = new FloatingItemDecoration(context);
        recyclerView.addItemDecoration(floatingItemDecoration);
        floatingItemDecoration
                .backGroudColor(context.getResources().getColor(R.color.hint_bg))
                .textSize(30 * WindowUtil.csw / WindowUtil.width)
                .titleHeight(40 * WindowUtil.csw / WindowUtil.width);
        return floatingItemDecoration;
    }

    public static FloatingItemDecoration openFloatDeco(Context context, RecyclerView recyclerView, List list, String field) {
        FloatingItemDecoration floatingItemDecoration = new FloatingItemDecoration(context, Color.GRAY, 1, 1);
        floatingItemDecoration
                .backGroudColor(context.getResources().getColor(R.color.hint_bg))
                .textSize(30 * WindowUtil.csw / WindowUtil.width)
                .titleHeight(40 * WindowUtil.csw / WindowUtil.width);
        recyclerView.addItemDecoration(floatingItemDecoration);
        HashMap<Integer, String> map = new HashMap<>();
        Flowable.fromIterable(list)
                .distinct(o -> getHeadField(o, field))
                .subscribe(o -> map.put(list.indexOf(o), getHeadField(o, field))
                        , e -> System.out.println(e)
                        , () -> floatingItemDecoration.setKeys(map)
                );

        return floatingItemDecoration;
    }

    public static void updateData(FloatingItemDecoration itemDecoration, List list, String field){
        HashMap<Integer, String> map = new HashMap<>();
        Flowable.fromIterable(list)
                .filter(o -> {
                    try {
                        getHeadField(o, field);
                    } catch (Exception e) {
                        return false;
                    }
                    return true;
                })
                .distinct(o -> getHeadField(o, field))
                .subscribe(o -> map.put(list.indexOf(o), getHeadField(o, field))
                        , e -> System.out.println(e)
                        , () -> itemDecoration.setKeys(map)
                );
    }
    public static PinHeadItemDecoration openPinDeco(Context context, RecyclerView recyclerView) {
        PinHeadItemDecoration floatingItemDecoration = new PinHeadItemDecoration();
//        floatingItemDecoration.setDecorationDrawer(new DecorationDrawer() {
//            @Override
//            public void drawVertical(Canvas canvas, String s, float v, float v1, float v2, float v3, Paint paint, Paint paint1, float v4, int i) {
//
//            }
//
//            @Override
//            public void drawFlow(Canvas canvas, String s, float v, float v1, float v2, float v3, Paint paint, Paint paint1, float v4, int i) {
//
//            }
//        });
        recyclerView.addItemDecoration(floatingItemDecoration);
        floatingItemDecoration
                .bgColor(context.getResources().getColor(R.color.hint_bg))
                .textSize(30 * WindowUtil.csw / WindowUtil.width)
                .titleHeight(40 * WindowUtil.csw / WindowUtil.width);
        return floatingItemDecoration;
    }

    public static PinHeadItemDecoration openPinDeco(Context context, RecyclerView recyclerView, List list, String field) {
        PinHeadItemDecoration floatingItemDecoration = new PinHeadItemDecoration();
        floatingItemDecoration
                .bgColor(context.getResources().getColor(R.color.hint_bg))
                .textSize(30 * WindowUtil.csw / WindowUtil.width)
                .titleHeight(40 * WindowUtil.csw / WindowUtil.width);
        recyclerView.addItemDecoration(floatingItemDecoration);
        HashMap<Integer, String> map = new HashMap<>();
        Flowable.fromIterable(list)
                .distinct(o -> getHeadField(o, field))
                .subscribe(o -> map.put(list.indexOf(o), getHeadField(o, field))
                        , e -> System.out.println(e)
                        , () -> floatingItemDecoration.setKeys(map)
                );

        return floatingItemDecoration;
    }

    public static void updateData(PinHeadItemDecoration itemDecoration,List list,String field){
        HashMap<Integer, String> map = new HashMap<>();
        Flowable.fromIterable(list)
                .filter(o -> {
                    try {
                        getHeadField(o, field);
                    } catch (Exception e) {
                        return false;
                    }
                    return true;
                })
                .distinct(o -> getHeadField(o, field))
                .subscribe(o -> map.put(list.indexOf(o), getHeadField(o, field))
                        , e -> System.out.println(e)
                        , () -> itemDecoration.setKeys(map)
                );
    }
    private static String getHeadField(Object o, String field) throws Exception {
        Field declaredField = o.getClass().getDeclaredField(field);
        declaredField.setAccessible(true);
        return PinYinUtil.head((String) declaredField.get(o));
    }
}
